
package com.sasis.dao.ibatis;

import java.sql.SQLException;
import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.VipTask;
import com.sasis.dao.VipTaskDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class VipTaskDaoiBatis extends BaseDaoiBATIS implements VipTaskDao {

    /**
     * @see com.sasis.dao.VipTaskDao#getVipTasks(com.sasis.model.VipTask)
     */
    public List getVipTasks(final VipTask vipTask) {
          return getSqlMapClientTemplate().queryForList("getVipTasks", null);
    }

    /**
     * @see com.sasis.dao.VipTaskDao#getVipTask(Integer id)
     */
    public VipTask getVipTask(Integer id) {
        VipTask vipTask = (VipTask) getSqlMapClientTemplate().queryForObject("getVipTask", id);

        if (vipTask == null) {
            throw new ObjectRetrievalFailureException(VipTask.class, id);
        }

        return vipTask;
    }

    public int getVipTaskMaxId() {
    	Integer vipTask = (Integer) getSqlMapClientTemplate().queryForObject("getVipTaskMaxId", null);

     
    	if(null==vipTask) return 0;
        return vipTask.intValue();
    }
    
    
    /**
     * @see com.sasis.dao.VipTaskDao#saveVipTask(VipTask vipTask)
     */    
    public int saveVipTask(final VipTask vipTask) {
        Integer id = vipTask.getId();
        // check for new record
        int flag = -1;
        if (id == null) {
        	try {
				getSqlMapClientTemplate().getSqlMapClient().startBatch();
				String ary = vipTask.getAry();
				String[] arys = ary.split(",");
				for(int i=0;i<arys.length;i++){
					String reqstId = arys[i];
					log.debug(">>>>>>>>>>>>>>>[request id]>>>>>>>>>>>>>>"+reqstId);
					vipTask.setRequestId(new Integer(reqstId));
					id = (Integer)getSqlMapClientTemplate().getSqlMapClient().insert("addVipTask", vipTask);
					flag++;
				}
				getSqlMapClientTemplate().getSqlMapClient().executeBatch();
			} catch (SQLException e) {
				e.printStackTrace();
			}
        	
            
            
        } else {
        	flag = getSqlMapClientTemplate().update("updateVipTask", vipTask);
        }
        
        return flag;
    }

    /**
     * @see com.sasis.dao.VipTaskDao#removeVipTask(Integer id)
     */
    public void removeVipTask(Integer id) {
        getSqlMapClientTemplate().update("deleteVipTask", id);
    }
}
